//
//  HomePageController.m
//  Food
//
//  Created by dllo on 15/7/13.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "HomePageController.h"
#import "BaseHeader.h"
#import "CycleScrollView.h"
#import "AFNetworkHandler.h"
#import "UIImageView+WebCache.h"
#import "HomePage_Bottom_model.h"
#import "HomePage_Collection_Cell.h"
#import "HomePageModel.h"
#import "HomePage_detailed_Controller.h"
#import "PageShowCollectionCell.h"
#import "Home_Symptomatic_diet.h"
#import "HomeIngredientsViewController.h"
#import "HomePage_display_Controller.h"
#import "HomePage_detailed_model.h"
#import "HomePage_selectdata_Controller.h"
@interface HomePageController ()<UICollectionViewDataSource, UICollectionViewDelegate>
// 分类数组
@property (nonatomic, retain) NSMutableArray *dataArr;

@property (nonatomic, retain) NSMutableArray *dataArry;

@property (nonatomic ,retain) UIView *myView;

@property (nonatomic, retain) UIButton *shutButton;

@property (nonatomic, retain) UIView *classView;

@property (nonatomic, retain) UICollectionView *showCollectionView;

@end

@implementation HomePageController

- (void)viewDidAppear:(BOOL)animated
{
    [UIView animateWithDuration:0.5 animations:^{
        self.view.window.alpha = 1.0;
    }];
}
- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
    self.navigationController.navigationBar.translucent = NO;
    self.collectionView.frame = CGRectMake(0, -160, self.view.frame.size.width, 160);
    
    self.classView.frame = CGRectMake(self.view.frame.size.width - 50, 0, 25, 70);
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.translucent = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.barTintColor= [UIColor colorWithRed:0.88 green:0.78 blue:0.68 alpha:1];

    self.navigationController.navigationBar.barTintColor= [UIColor colorWithRed:238 / 255.0 green:236 / 255.0 blue:232 / 255.0 alpha:1];
    self.navigationItem.title = @"首页";
    
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"backGround.jpg"]];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(selectData)];
    
    [self createShowCollectionView];
    [self createCollectionView];
    [self getData];
    [self getdata];
    [self createClassView];
}

- (void) selectData
{
    HomePage_selectdata_Controller *select = [[HomePage_selectdata_Controller alloc] init];
    [self.navigationController pushViewController:select animated:YES];
}

//
- (void) getdata
{
   
    UIActivityIndicatorView *activityIndicator = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge] autorelease];
    
    activityIndicator.frame = CGRectMake(0, 0, 100, 100);
    
    activityIndicator.color = [UIColor grayColor];
    
    activityIndicator.center = CGPointMake(ScreenWidth / 2.0, ScreenHeight / 2.0 - 50);
    
    [self.view addSubview:activityIndicator];
    
    [activityIndicator startAnimating];
    
    self.dataArr = [NSMutableArray array];
    NSString *urlStr = @"http://121.41.84.91:80/HandheldKitchen/api/more/tblcalendaralertinfo!getHomePage.do?phonetype=2&page=10&pageRecord=10&user_id=&is_traditional=0";
    [AFNetworkHandler getWithAFNByURL:urlStr completion:^(id result) {
        NSArray *tempArr =result[@"data"];
        self.dataArr = [HomePage_Bottom_model arrayWithModelByArray:tempArr];
        [self.showCollectionView reloadData];
        [activityIndicator stopAnimating];
    }];
}
// 分类
- (void) getData
{
    self.dataArry = [NSMutableArray array];
    NSString *str = @"http://121.41.84.91:80/HandheldKitchen/api/home/tblAssort!getFirstgrade.do";
    [AFNetworkHandler getWithAFNByURL:str completion:^(id result) {
        NSArray *tempArr =result[@"data"];
        self.dataArry = [HomePageModel arrayWithModelByArray:tempArr];
        [self.collectionView reloadData];
    }];
}


- (void)createShowCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(ScreenWidth, ScreenHeight - 64 - 49);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.minimumLineSpacing = 0;
    
    self.showCollectionView  = [[[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - 64 - 49) collectionViewLayout:layout] autorelease];
    self.showCollectionView.contentOffset = CGPointMake(0, 0);
    self.showCollectionView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"backGround.jpg"]];
    
    self.showCollectionView.pagingEnabled = YES;
    self.showCollectionView.tag = 2002;
    self.showCollectionView.delegate = self;
    self.showCollectionView.dataSource = self;
    [self.showCollectionView registerClass:[PageShowCollectionCell class] forCellWithReuseIdentifier:@"reuse"];
    [self.view addSubview:self.showCollectionView];
}
/**
 *   分类的滚动collectionView;
 */
- (void)createCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((self.view.frame.size.width - 50) / 4.0, 70);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.sectionInset = UIEdgeInsetsMake(5, 10, 5, 10);
    //初始化CollectionView
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, - 160, self.view.frame.size.width, 150) collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"backGround.jpg"]];
    self.collectionView.tag = 2001;
    //添加重用池
    [self.collectionView registerClass:[HomePage_Collection_Cell class] forCellWithReuseIdentifier:@"reuse"];
    // 上划手势
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(shutButtonActin)];
    [self.collectionView addGestureRecognizer:swipe];
    swipe.direction = UISwipeGestureRecognizerDirectionUp;
    [swipe release];

    [self.view addSubview:self.collectionView];
    [self createShutButton];
}


/**
 *  myView 上的button
 */
- (void) createShutButton
{
    self.shutButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.shutButton.frame = CGRectMake(self.collectionView.frame.size.width / 2 - 10, -15, 20, 15);
    [self.shutButton.layer setCornerRadius:self.shutButton.frame.size.width / 2];
    [self.shutButton setImage:[UIImage imageNamed:@"shang.png"] forState:UIControlStateNormal];
    
    [self.shutButton addTarget:self action:@selector(shutButtonActin) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.shutButton];
}

/**
 *  shutButton单击方法
 */
- (void)shutButtonActin
{
    [UIView animateWithDuration:0.5 animations:^{
        self.collectionView.frame = CGRectMake(0, -160, self.view.frame.size.width, 160);
        self.shutButton.frame = CGRectMake(self.collectionView.frame.size.width / 2 - 10, -15, 20, 15);
        NSLog(@"sdfsjla");
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.4 animations:^{
            self.classView.frame = CGRectMake(self.view.frame.size.width - 50, 0, 25, 70);
        }];
    }];
}

/**
 *  分类按钮试图
 */
- (void) createClassView
{
    self.classView = [[UIView alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 50, 0, 25, 70)];
    
    UIView *erectView = [[UIView alloc] initWithFrame:CGRectMake((self.classView.frame.size.width / 2) - 0.5, 0, 1, 45)];
    erectView.backgroundColor = [UIColor orangeColor];
    [self.classView addSubview:erectView];
    
    UIImageView *classImag = [[UIImageView alloc] initWithFrame:CGRectMake(0, 45, 25, 25)];
    classImag.image = [UIImage imageNamed:@"fenlei"];
    [classImag.layer setCornerRadius:25 / 2];
    
    [classImag setUserInteractionEnabled:YES];
    UITapGestureRecognizer *tap = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)] autorelease];
    
    [classImag addGestureRecognizer:tap];
    tap.numberOfTapsRequired = 1;
    tap.numberOfTouchesRequired = 1;
    
    [self.classView addSubview:classImag];
    [self.view addSubview:self.classView];
    
}

- (void)tapAction:(UITapGestureRecognizer *)tap
{
    [UIView animateWithDuration:0.4 animations:^{
        self.classView.frame = CGRectMake(self.view.frame.size.width - 50, -70, 25, 70);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.5 animations:^{
            self.collectionView.frame = CGRectMake(0, 0, self.view.frame.size.width, 160);
            self.shutButton.frame = CGRectMake(self.collectionView.frame.size.width / 2 - 10, self.collectionView.frame.size.height - 15, 20, 15);
        }];
    }];
}



- (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (collectionView.tag == 2001) {
        return self.dataArry.count - 2;
    } else if(collectionView.tag == 2002) {
        return self.dataArr.count;
    }
    return 0;
}


- (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView.tag == 2001) {
        HomePage_Collection_Cell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
        HomePageModel *model = self.dataArry[indexPath.item];
        NSString *str = [NSString stringWithFormat:@"%ld.jpg", indexPath.item + 1];
        cell.imageView.image = [UIImage imageNamed:str];
        cell.lable.text = model.name;
        return cell;
    } if (collectionView.tag == 2002) {
        PageShowCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
        cell.bottomModel = self.dataArr[indexPath.item];
        return cell;
    }
    return nil;
}



-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView.tag == 2001) {

        if (indexPath.item == 19) {
            Home_Symptomatic_diet *symptomaticDiet = [[[Home_Symptomatic_diet alloc] init] autorelease];
            HomePageModel *model = self.dataArry[indexPath.item];
            symptomaticDiet.arr = model.detaoledArry;
            symptomaticDiet.title = [model.detaoledArry[0] objectForKey:@"name"];
            [self.navigationController pushViewController:symptomaticDiet animated:YES];
        } else if (indexPath.item == 22) {
            HomeIngredientsViewController *imgredientsContrller = [[[HomeIngredientsViewController alloc] init] autorelease];
            HomePageModel *model = self.dataArry[indexPath.item];
            imgredientsContrller.arr = model.detaoledArry;
            imgredientsContrller.title = [model.detaoledArry[0] objectForKey:@"name"];
            [self.navigationController pushViewController:imgredientsContrller animated:YES];
        } else {
            HomePage_detailed_Controller *HPdController = [[[HomePage_detailed_Controller alloc] init] autorelease];
            HomePageModel *model = self.dataArry[indexPath.item];
            HPdController.dataArry = model.detaoledArry;
            HPdController.title = model.name;
            [self.navigationController pushViewController:HPdController animated:YES];
        }

    } else if(collectionView.tag == 2002) {
        HomePage_display_Controller *display = [[[HomePage_display_Controller alloc] init] autorelease];
        HomePage_Bottom_model *model = self.dataArr[indexPath.item];
        display.classid = model.vegetable_id;
        display.classTitle = model.name;
        display.MVBackGroundimage = model.imagePathLandscape;
        [self.navigationController pushViewController:display animated:YES];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
